package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.SolutionRankListReq;
import com.homedo.odin.solution.contract.mng.SolutionRankListResp;
import com.homedo.odin.solution.enumcode.solution.RankTypeEnum;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.service.bizservice.mng.MngSolutionRankBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 方案精品排行榜
 *
 * @author: liyi
 * @date: 2018/5/17 17:30
 * @Description:
 */
@Api(description = "方案", tags = {"Solution"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng")
public class MngSolutionRankController extends BaseController<MngSolutionRankBizService> {

    @ApiOperation(value = "OMS_获取热搜列表", notes = "OMS_获取热搜列表", response = SolutionRankListResp.class, httpMethod = "GET")
    @GetMapping("/solutions/hot_search")
    public ResultData getHotsearch() {
        SolutionRankListResp resp = baseService.querySolutionRankHot();
        return ResultData.newInstance(Meta.CODE_200, resp);
    }

    @ApiOperation(value = "OMS_获取精品列表", notes = "OMS_获取精品列表", response = SolutionRankListResp.class, httpMethod = "GET")
    @GetMapping("/solutions/extractive")
    public ResultData getSolutionRank() {
        SolutionRankListResp resp = baseService.querySolutionRank(RankTypeEnum.JINGPING.getDbValue());
        return ResultData.newInstance(Meta.CODE_200, resp);
    }

    @ApiOperation(value = "OMS_批量修改热搜(前台运营)", notes = "OMS_批量修改热搜(前台运营)", httpMethod = "POST")
    @PostMapping("/solutions/hot_search")
    public ResultData updateSolutionRank(@Valid @RequestBody SolutionRankListReq req, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        List<Meta> metaList = baseService.updateSolutionRank(req);
        return ResultData.newInstance(new Meta(Meta.CODE_200.getCode(), "操作方案热搜榜成功"), metaList);
    }

    @ApiOperation(value = "OMS_批量修改精品(前台运营)", notes = "OMS_批量修改精品(前台运营)", httpMethod = "POST")
    @PostMapping("/solutions/extractive")
    public ResultData updateSolutionExtractiveRank(@Valid @RequestBody SolutionRankListReq req,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        List<Meta> metaList = baseService.updateSolutionExtractiveRank(req);
        return ResultData.newInstance(new Meta(Meta.CODE_200.getCode(), "操作方案热搜榜成功"), metaList);
    }


    @ApiOperation(value = "OMS_查询方案是否存在(精品热搜方案榜管理  具体方案查询)", notes = "OMS_查询方案是否存在（精品热搜方案榜管理  具体方案查询）", response = Solution.class, httpMethod = "GET")
    @GetMapping("/solutions/extractive/{solutionId}")
    public ResultData getSolutionById(@ApiParam(value = "方案ID", required = true) @PathVariable Long solutionId) {
        if (!NumberHelper.validate(solutionId)) {
            throw new BizException(Meta.CODE_451.getCode(), "参数异常");
        }
        Solution solution = baseService.getSolutionById(solutionId);
        return ResultData.newInstance(new Meta(Meta.CODE_200.getCode(), "搜索该方案成功"), solution);
    }

}